package com.freemyleft.left.left_app.mian.index.work.waitingforclass;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.freemyleft.left.left_app.R;
import com.freemyleft.left.left_app.R2;
import com.freemyleft.left.left_app.mian.index.work.completecoures.CourseDetailsDelegate;
import com.freemyleft.left.left_app.mian.index.work.waitingforclass.adapter.WaitingForClassAdapter;
import com.freemyleft.left.left_app.mian.index.work.waitingforclass.adapter.WaitingForClassConvert;
import com.freemyleft.left.left_app.mian.index.work.waitingforclass.adapter.WaitingForClassFiles;
import com.freemyleft.left.zapp.delegates.LeftDelegate;
import com.freemyleft.left.zapp.net.RestClient;
import com.freemyleft.left.zapp.net.callback.IError;
import com.freemyleft.left.zapp.net.callback.IFailure;
import com.freemyleft.left.zapp.net.callback.ISuccess;
import com.freemyleft.left.zapp.ui.PickerViewUtils;
import com.freemyleft.left.zapp.ui.dialog.MyPhoneDiaLog;
import com.freemyleft.left.zapp.ui.recycler.MultipleItemEntity;
import com.freemyleft.left.zapp.util.LeftLogger;
import com.freemyleft.left.zapp.util.LeftPreference;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Logger;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 待上课
 */

public class WaitingForClassDelegate extends LeftDelegate implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R2.id.tv_select_date)
    TextView selectDate;
    @BindView(R2.id.recy)
    RecyclerView recyclerView;
    @BindView(R2.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private MyPhoneDiaLog myPhoneDiaLog;
    private ArrayList<MultipleItemEntity> data;
    String strattime;
    private String id;
    private String token;
    private String teacherId;


    @OnClick(R2.id.tv_select_date)
    void onClickselectDete() {
        PickerViewUtils.showDetePickerView(selectDate, getContext());
        strattime = selectDate.getText().toString();
        if (strattime != selectDate.getText().toString()) {
            inintData();
        }


    }


    @Override
    public Object setLayout() {
        return R.layout.delegate_work_waiting_for_class;
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);


    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        strattime = selectDate.getText().toString();
        long current = System.currentTimeMillis();
        Date date = new Date();
        date.setTime(current);
        selectDate.setText(PickerViewUtils.DateToStr(date));
        strattime = selectDate.getText().toString();
        refreshLayout.setOnRefreshListener(this);
        inintData();
    }

    private void inintData() {

        token = LeftPreference.getCustomAppProfile(TOKEN);
        teacherId = LeftPreference.getCustomAppProfile(TEACHER_ID);
        RestClient.builder().url("/home/teacher/work_list")
                .loader(getContext())
                .params("teacherid", teacherId)
                .params("token", token)
                .params("type", 1)
                .params("startime", strattime)
                .params("limitnum", 10)
                .params("page", 1)
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String responnse) {
                        JSONObject object = JSON.parseObject(responnse);
                        if (object.getString("code").equals("1000")) {
                            Log.e("responnse", responnse.toString());
                            refreshLayout.setRefreshing(false);
                            if (data != null) {
                                data.clear();
                            }
                            data = new WaitingForClassConvert().setJsonData(responnse).convert();
                            LeftLogger.e("WaitingForClassDelegate", responnse);
                            WaitingForClassAdapter adapter = new WaitingForClassAdapter(data);
                            Log.e("WaitingForClassDelegate", data.toString());
                            LinearLayoutManager manager = new LinearLayoutManager(getContext());
                            manager.setOrientation(LinearLayoutManager.VERTICAL);
                            recyclerView.setAdapter(adapter);
                            recyclerView.setLayoutManager(manager);
                            adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {

                                @Override
                                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                    MultipleItemEntity entity
                                            = (MultipleItemEntity) adapter.getData().get(position);
                                    id = entity.getField(WaitingForClassFiles.ID);
                                    if (view.getId() == R.id.tv_yuxi) {
                                        getParentDelegate().getParentDelegate().start(new HomeworkDelegate());
                                    }
                                    if (view.getId() == R.id.imag_communication) {
                                        communication();
                                    }
                                    if (view.getId() == R.id.wancheng) {
                                        CarryOut();
                                    }
                                }
                            });
                            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    getParentDelegate().getParentDelegate().start(new CourseDetailsDelegate());
                                }
                            });
                        }
                    }
                }).failure(new IFailure() {
            @Override
            public void onFailure() {
                Toast.makeText(_mActivity, "onfailure", Toast.LENGTH_SHORT).show();

            }
        }).error(new IError() {
            @Override
            public void onError(int code, String msg) {
                Toast.makeText(_mActivity, msg, Toast.LENGTH_SHORT).show();
            }
        }).buid().post();
    }

    private void CarryOut() {
        RestClient.builder()
                .url("/home/teacher/course_statu")
                .loader(getContext())
                .params("token", token)
                .params("teacherid", teacherId)
                .params("studentid", id)
                .params("type", 1)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String responnse) {
                        JSONObject jsonObject = JSONObject.parseObject(responnse);
                        if (jsonObject.getString("code").equals("1000")) {
                            Toast.makeText(_mActivity, jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
                        } else if (jsonObject.getString("code").equals("1001")) {
                            Toast.makeText(_mActivity, jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
                        }
                    }
                }).buid().post();

    }


    private void communication() {
        myPhoneDiaLog = new MyPhoneDiaLog(getContext());

        myPhoneDiaLog.setYesOnclickListener("在线沟通", new MyPhoneDiaLog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                Toast.makeText(getContext(), "在线沟通", Toast.LENGTH_SHORT).show();
                myPhoneDiaLog.dismiss();
            }
        });
        myPhoneDiaLog.setNoOnclickListener("电话沟通", new MyPhoneDiaLog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                //应该是每个老师会给传一个电话
                Uri data = Uri.parse("tel:" + 000000000);
                intent.setData(data);
                startActivity(intent);
                Toast.makeText(getContext(), "电话沟通", Toast.LENGTH_SHORT).show();
                myPhoneDiaLog.dismiss();
            }
        });
        myPhoneDiaLog.setdismissOnclickListener("取消", new MyPhoneDiaLog.onDismissOnclickListener() {
            @Override
            public void onDismissClick() {
                myPhoneDiaLog.dismiss();
            }
        });
        myPhoneDiaLog.show();
    }

    @Override
    public void onRefresh() {
        inintData();

    }
}
